﻿using System;
using System.Collections.Generic;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS.Web;
using IFRS_Global;

public partial class IFRS_WIN10_2E : PageBase
{
    #region === Event ===

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewMenu();
            BindControl();
            BindData();
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }
    protected void btnCONFIRM_Click(object sender, EventArgs e)
    {
        SaveData(true);
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN10_1S.aspx");
    }

    #endregion

    #region === Method ===

    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN10_2E;
    }
    private void BindControl()
    {
        btnSAVE.ValidationGroup = "Master";

        reqTransactionDate.SetRequiredField(lblTransactionDate, txtTransactionDate, btnSAVE);
        reqAccountNo.SetRequiredField(lblAccountNo, txtAccountNo, btnSAVE);
        reqGLAccount.SetRequiredField(lblGLAccount, txtGLAccountCode, btnSAVE);
        reqCostCenter.SetRequiredField(lblCostCenter, txtCostCenter, btnSAVE);

        reqContractStartDate.SetRequiredField(lblContractStartDate, txtContractStartDate, btnSAVE);
        reqContractEndDate.SetRequiredField(lblContractEndDate, txtContractEndDate, btnSAVE);

        reqPaymentConditionID.SetRequiredField(lblPaymentConditionID, ddlPaymentConditionID, btnSAVE);
        reqCurrencyCode.SetRequiredField(lblCurrencyCode, txtCurrencyCode, btnSAVE);

        reqLoanAmount.SetRequiredField(lblLoanAmount, txtLoanAmount, btnSAVE);

        reqCustomerID.SetRequiredField(lblCustomerID, txtCustomerID, btnSAVE);
        reqContractLoanTypeID.SetRequiredField(lblContractLoanTypeID, ddlContractLoanTypeID, btnSAVE);

        reqLoanObjectiveID.SetRequiredField(lblLoanObjectiveID, txtLoanObjectiveID, btnSAVE);

        ddlPaymentConditionID.SetData(DDL_TYPE.PaymentCondition, string.Empty, false, true);
        ddlContractLoanTypeID.SetData(DDL_TYPE.ContractLoanType, "", false, true);

        btnCustomerID.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/Customer.aspx") + "','700','550');");
        txtCustomerID.AddControl(hddCustomerID.ClientID);
        txtCustomerID.AddControl(txtCustomerID.ClientID);
        txtCustomerID.ControlSearch = txtCustomerID.ClientID;
        txtCustomerID.SearchKey = "Customer_oCustomerID_oCustomerCode";

        btnGLAccountCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/GLAccount.aspx") + "','700','550');");
        txtGLAccountCode.AddControl(txtGLAccountCode.ClientID);
        txtGLAccountCode.ControlSearch = txtGLAccountCode.ClientID;
        txtGLAccountCode.SearchKey = "GLAccount_oGLAccountCode_oGLAccountName";

        btnCostCenter.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/CostCenter.aspx") + "','700','550');");
        txtCostCenter.AddControl(hddCostCenter.ClientID);
        txtCostCenter.AddControl(txtCostCenter.ClientID);
        txtCostCenter.ControlSearch = txtCostCenter.ClientID;
        txtCostCenter.SearchKey = "VWCostCenter_oCOST_CENTER_CD_oCOST_CENTER_NM";

        btnLoanObjectiveID.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/VmLoanObjective.aspx") + "','700','550');");
        txtLoanObjectiveID.AddControl(hddLoanObjectiveID.ClientID);
        txtLoanObjectiveID.AddControl(txtLoanObjectiveID.ClientID);
        txtLoanObjectiveID.ControlSearch = txtLoanObjectiveID.ClientID;
        txtLoanObjectiveID.SearchKey = "VmLoanObjective_oCBT_CUST_BUSINESS_TYPE_ID_oCBT_DESC_LEV1";

        btnCurrencyCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/Currency.aspx") + "','700','550');");
        txtCurrencyCode.AddControl(txtCurrencyCode.ClientID);
        txtCurrencyCode.ControlSearch = txtCurrencyCode.ClientID;
        txtCurrencyCode.SearchKey = "Currency_oCurrencyCode_oCurrencyName";

        txtStatus.Text = Resources.ifrsField.CREATE;
    }
    private void BindData()
    {
        if (Convert.ToString(Request.QueryString["id"]) != string.Empty)
        {
            var id = Convert.ToInt32(Request.QueryString["id"]);
            var dt = new SPR_DISCOUNTLOAN().SEARCH_BYID(id);
            if (dt != null && dt.Rows.Count > 0)
            {
                var list = new List<DiscountLoan>();
                ConvertHelper.ConvertDataTableToObject(dt, list);

                txtTransactionDate.Text = list[0].TransactionDate.DateToString();
                txtAccountNo.Text = list[0].AccountNo;

                txtGLAccountCode.Text = list[0].GLAccount;

                hddCostCenter.Value = list[0].CostCenter;
                txtCostCenter.Text = list[0].COST_CENTER_NM;

                txtContractStartDate.Text = list[0].ContractStartDate.DateToString();
                txtContractEndDate.Text = list[0].ContractEndDate.DateToString();
                ddlPaymentConditionID.SelectedValue = Convert.ToString(list[0].PaymentConditionID);
                txtCurrencyCode.Text = list[0].CurrencyCode;
                txtPreReceivedInterest.Text = Convert.ToString(list[0].PreReceivedInterest);
                txtLoanAmount.Text = Convert.ToString(list[0].LoanAmount);

                hddCustomerID.Value = Convert.ToString(list[0].CustomerID);
                txtCustomerID.Text = list[0].CustomerCode;

                ddlContractLoanTypeID.SelectedValue = Convert.ToString(list[0].ContractLoanTypeID);

                hddLoanObjectiveID.Value = list[0].LoanObjectiveID;
                txtLoanObjectiveID.Text = list[0].CBT_DESC_LEV1;

                txtMisPaymentDate.Text = list[0].MisPaymentDate.DateToString();

                txtApprovedAmount.Text = Convert.ToString(list[0].ApprovedAmount);
                txtReservedAmount.Text = Convert.ToString(list[0].ReservedAmount);
                txtCollateralAmount.Text = Convert.ToString(list[0].CollateralAmount);

                chkDebtAdjustFlag.Checked = !string.IsNullOrEmpty(list[0].DebtAdjustFlag) &&
                                            list[0].DebtAdjustFlag.Trim() == "Y";

                if (list[0].IsConfirmed != true) return;
                JS_Alert(Resources.ifrsResource.Confirmed, ResolveUrl(string.Format("~/IFRS/WIN10_3V.aspx?&id={0}", list[0].DiscountLoanID)));
            }
        }
    }

    private void SaveData(bool isConfirmed = false)
    {
        if (!Page.IsValid) return;
        if (!ValidationDateNotEqual(txtContractStartDate.Text.StringToDate(), txtContractEndDate.Text.StringToDate()))
        {
            JS_Alert(string.Format("{0} {1}{2} {3}", lblContractEndDate.Text, Resources.ifrsResource.Must, Resources.ifrsResource.Over, lblContractStartDate.Text));
            return;
        }

        var dal = new SPR_DISCOUNTLOAN();
        var models = new DiscountLoan();

        models.DiscountLoanID = Convert.ToInt32(Request.QueryString["id"]);

        models.TransactionDate = txtTransactionDate.Text.StringToDate();
        models.AccountNo = txtAccountNo.Text;

        models.GLAccount = txtGLAccountCode.Text;
        models.CostCenter = hddCostCenter.Value;

        models.ContractStartDate = txtContractStartDate.Text.StringToDate();
        models.ContractEndDate = txtContractEndDate.Text.StringToDate();

        models.PaymentConditionID = ddlContractLoanTypeID.SelectedValue.AsInt32();
        models.CurrencyCode = txtCurrencyCode.Text;
        models.PreReceivedInterest = txtPreReceivedInterest.Text.AsDecimal();
        models.LoanAmount = txtLoanAmount.Text.AsDecimal();
        models.CustomerID = hddCustomerID.Value.AsInt32();
        models.ContractLoanTypeID = ddlContractLoanTypeID.SelectedValue.AsInt32();
        models.LoanObjectiveID = hddLoanObjectiveID.Value;
        models.MisPaymentDate = txtMisPaymentDate.Text.StringToDateAllowNull();
        models.ApprovedAmount = txtApprovedAmount.Text.AsDecimal();
        models.ReservedAmount = txtReservedAmount.Text.AsDecimal();
        models.CollateralAmount = txtCollateralAmount.Text.AsDecimal();

        models.DebtAdjustFlag = chkDebtAdjustFlag.Checked ? "Y" : string.Empty;

        models.IsConfirmed = isConfirmed;

        models.RP_GSB_PRDCD = DefaultValue.RP_GSB_PRDCD.ADVINT;
        models.RP_SOURCE_FILE = DefaultValue.RP_SOURCE_FILE.MANUAL_ADVINT;
        models.RP_MAPPING_CD = DefaultValue.RP_MAPPING_CD.ADVINT;
        models.RP_GSB_ASSTCLS = DefaultValue.RP_GSB_ASSTCLS.One;

        models.SetStandardField();

        var result = dal.UPDATE(models);
        if (result > 0)
        {
            if (isConfirmed)
                JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN10_3V.aspx?&id={0}", models.DiscountLoanID)));
            else
                JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN10_2E.aspx?&id={0}", models.DiscountLoanID)));
                
        }
        else // Duplicate
            Utility.AlertMessage(Page, result == -1 ? Resources.ifrsResource.Duplicate : Resources.ifrsResource.CanNotSaveData);
    }

    #endregion
}